#include "resultwidget.h"
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QDebug>
#include <QMessageBox>
#include <QDesktopServices>
#include "customtableview.h"

resultwidget::resultwidget(QWidget *parent) : QWidget(parent)
{
    m_ptaskinfo = NULL;
    m_itemsselectedmap.clear();
    initControl();
    setLayoutWidget();
    showData();
}

resultwidget::~resultwidget()
{
    releseData();
}

void resultwidget::initControl()
{
    m_tableview = new resulttableview(this);
    QStringList header;
    // header << "序号" << "任务名称" << "任务类型" << "任务状态" << "关键词名称" << "网址" << "内容" << "电话" << "标记" << "查询页数";
    header << u8"test1" << u8"test2" << u8"test3" << u8"test4" << u8"test5" << u8"test6" << u8"test7" << u8"test7" << u8"test7" << u8"test7";
    m_tableview->setHHeader(header);

    //设置字体特殊颜色
    m_tableview->SetItemTextColor(u8"查询完成", QColor(0, 191, 255));
    m_tableview->SetItemTextColor(u8"查询停止", QColor(255, 0, 0));
    m_tableview->SetItemTextColor(u8"test", QColor(50, 205, 50));
    m_tableview->SetItemTextColor(u8"no mark", QColor(255, 0, 0));

    connect(m_tableview->selectionModel(), SIGNAL(selectionChanged(QItemSelection, QItemSelection)),
            this, SLOT(selectionChanged(QItemSelection, QItemSelection)));
    connect(m_tableview, SIGNAL(headerSortClicked(int, int)), this, SLOT(headerSortClicked(int, int)));
    connect(m_tableview, SIGNAL(btnClicked(int, int)), this, SLOT(btnClicked(int, int)));
}

void resultwidget::setLayoutWidget()
{

}

void resultwidget::resizeEvent(QResizeEvent *event)
{
    QWidget::resizeEvent(event);
    m_tableview->setGeometry(0, 0, this->rect().width(), this->rect().height());
    int iwidth = 1300;
    int icolumn = 10;
    m_tableview->setColumnWidth(0, 80);
    for(int index = 1; index < icolumn; index++)
        m_tableview->setColumnWidth(index, iwidth / icolumn);
}

void resultwidget::paintEvent(QPaintEvent *event)
{
    QWidget::paintEvent(event);
}

void resultwidget::showData(QString orderby, QString sorttype)
{
    releseData();
    m_tableview->clear();

    //排序方式
    if(orderby.isEmpty())
        orderby = "id";
    //降序或升序
    if(sorttype.isEmpty())
        sorttype = "ASC";
    int itotal = 0;
    QString str_sql =  "SELECT COUNT(*) AS count FROM result;";
    QSqlQuery query;
    query.setForwardOnly(true);
    bool success = query.exec(str_sql);
    if(!success)
    {
        /*  QMessageBox::information(this, "提示", "查询任务数失败");
          return;*/
    }
    int icount = 0;
    if(query.next())
    {
        icount = query.value(0).toInt();
    }
    if(icount <= 0)
    {
        return;
    }
    else
    {
        itotal = icount;
    }

    str_sql = "SELECT id,taskname,tasktype,taskstatus,keywordname,url,title,tel,mark,page FROM result";

    if (!orderby.isEmpty() && !sorttype.isEmpty())
    {
        str_sql += " ORDER BY " + orderby + " " + sorttype;
    }

    m_ptaskinfo = new STRUCT_TASK_INFO[itotal];
    success = query.exec(str_sql);
    if(!success)
    {
        QMessageBox::information(this, "提示", "查询任务数据失败");
        return;
    }
    int index = 0;
    while(query.next())
    {
        m_ptaskinfo[index].id = query.value(0).toString();
        m_ptaskinfo[index].taskname = query.value(1).toString();
        m_ptaskinfo[index].tasktype = query.value(2).toString();
        m_ptaskinfo[index].taskstatus = query.value(3).toString();
        m_ptaskinfo[index].keywordname = query.value(4).toString();
        m_ptaskinfo[index].url = query.value(5).toString();
        m_ptaskinfo[index].title = query.value(6).toString();
        m_ptaskinfo[index].tel = query.value(7).toString();
        m_ptaskinfo[index].mark = query.value(8).toString();
        m_ptaskinfo[index].page = query.value(9).toString();

        QStringList rowlist;
        rowlist.append("");//第一列为序号，序号数自动生成，传入""即可
        rowlist.append(query.value(1).toString());
        rowlist.append(query.value(2).toString());
        rowlist.append(query.value(3).toString());
        rowlist.append(query.value(4).toString());
        rowlist.append(query.value(5).toString());
        rowlist.append(query.value(6).toString());
        rowlist.append(query.value(7).toString());
        rowlist.append(query.value(8).toString());
        rowlist.append(query.value(9).toString());
        m_tableview->addRow(rowlist);
        m_tableview->setFreezeRowHeight(index, 25); //设置冻结表格行高
        index++;
    }
    setItemsSelected();
}

void resultwidget::releseData()
{
    if (m_ptaskinfo != NULL)
    {
        delete[] m_ptaskinfo;
        m_ptaskinfo = NULL;
    }
}

void resultwidget::selectionChanged(const QItemSelection &selected, const QItemSelection &deselected)
{
    QModelIndexList selectrowslist = m_tableview->selectionModel()->selectedRows();
    m_itemsselectedmap.clear();
    for(int i = 0; i < selectrowslist.count(); i++)
    {
        m_itemsselectedmap.insert(m_ptaskinfo[selectrowslist[i].row()].id.toInt(), 1);
    }
}

void resultwidget::setItemsSelected()
{
    m_tableview->clearSelection();
    int totalcloumn = m_tableview->columnCount();
    int totalrow = m_tableview->rowCount();
    QItemSelection itemsselection;
    QModelIndexList selectedlist;
    for(int i = 0; i < totalrow; i++)
    {
        if(m_itemsselectedmap.find(m_ptaskinfo[i].id.toInt()) != m_itemsselectedmap.end())
        {
            QModelIndex leftindex = m_tableview->model()->index(i, 0);
            selectedlist.append(leftindex);
            QModelIndex rightindex = m_tableview->model()->index(i, totalcloumn - 1);
            QItemSelectionRange selectionrange(leftindex, rightindex);
            itemsselection.append(selectionrange);
        }
    }
    m_tableview->setIconSelection(selectedlist); //设置icon选中图片列表
    m_tableview->selectionModel()->select(itemsselection, QItemSelectionModel::Select); //设置行选中
}

void resultwidget::headerSortClicked(int sort, int column)
{
    dataSort(sort, column);
}

void resultwidget::dataSort(int sort, int column)
{
    QString orderby = "", sorttype = "";
    if (sort == 2)
        sorttype = "ASC";
    else
        sorttype = "DESC";

    switch(column)
    {
    case taskname:
        orderby = "taskname";
        break;
    case tasktype:
        orderby = "tasktype";
        break;
    case taskstatus:
        orderby = "taskstatus";
        break;
    case keywordname:
        orderby = "keywordname";
        break;
    case url:
        orderby = "url";
        break;
    case title:
        orderby = "title";
        break;
    case tel:
        orderby = "tel";
        break;
    case mark:
        orderby = "mark";
        break;
    case page:
        orderby = "page";
        break;
    }
    if (orderby == "")
        return;
    showData(orderby, sorttype);
}

void resultwidget::btnClicked(int row, int column)
{
    QString strurl = QString("http://%1").arg(m_ptaskinfo[row].url);
    QDesktopServices::openUrl(QUrl(strurl));
}





















